home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / InputSprocket / Sample Drivers / ISp Sample USB Sources / ISpSampleUSB.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-23  |  12.5 KB  |  479 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2.  
  3. File:      ISpSampleUSB.h
  4.  
  5. Copyright © 1996, 1997, 1998 Apple Computer, Inc., All Rights Reserved
  6.  
  7.  
  8. You may incorporate this sample code into your applications without
  9. restriction, though the sample code has been provided "AS IS" and the
  10. responsibility for its operation is 100% yours.  However, what you are
  11. not permitted to do is to redistribute the source as "DSC Sample Code"
  12. after having made changes. If you're going to re-distribute the source,
  13. we require that you make it clear in the source that the code was
  14. descended from Apple Sample Code, but that you've made changes.
  15.  
  16. *************************************************************************************/
  17.  
  18.  
  19. #ifndef __ISPSAMPLEUSB__
  20. #define __ISPSAMPLEUSB__
  21.  
  22. #ifndef __MACTYPES__
  23. #include <MacTypes.h>
  24. #endif
  25.  
  26. #ifndef __DESKBUS__
  27. #include <DeskBus.h>
  28. #endif
  29.  
  30. #ifndef __FILES__
  31. #include <Files.h>
  32. #endif
  33.  
  34. #ifndef __INPUTSPROCKET__
  35. #include <InputSprocket.h>
  36. #endif
  37.  
  38. #include "UniversalHIDModule.h"
  39.  
  40. #include "ISpPlugInUI.h"
  41. #include "ISpSampleUSBRes.h"
  42. #include "ListUtils.h"
  43.  
  44. // these constants are used to describe specific values for an element (a usbElementKey)
  45. enum
  46. {
  47.     // it is important that all buttons come first and start at 1
  48.     kUSB_button1_element = 1,
  49.     kUSB_button2_element,
  50.     kUSB_button3_element,
  51.     kUSB_button4_element,
  52.     kUSB_button5_element,
  53.     kUSB_button6_element,
  54.     kUSB_button7_element,
  55.     kUSB_button8_element,
  56.     kUSB_trigger_element,
  57.     kUSB_buttonStart_element,
  58.     kUSB_buttonSelect_element,
  59.     kUSB_povhat_element,
  60.     kUSB_povhat4_element,
  61.     kUSB_dpad_element,
  62.     kUSB_dpad4_element,
  63.     kUSB_xaxis_element,
  64.     kUSB_yaxis_element,
  65.     kUSB_throttle_element,
  66.     kUSB_rudder_element,
  67.     kUSB_trim_element,
  68.     kUSB_gas_element,
  69.     kUSB_brake_element,
  70.     kUSB_axis_element
  71. };
  72.  
  73. class ISpUSBDevice : public ISpPlugInUI
  74. {
  75.     public:
  76.         ISpUSBDevice(const UHIDModuleDispatchTablePtr hidmoduleDispatchTable,
  77.                      const USBDeviceRef usbDeviceRef, const FSSpec &inFileSpec);
  78.         virtual ~ISpUSBDevice();
  79.         
  80.         void FinishConstruction(void);
  81.         
  82.     public:
  83.         // these must be overridden in the subclass
  84.         OSStatus Init(    UInt32 count, 
  85.                                 ISpNeed needs[], 
  86.                                 ISpElementReference virtualElements[],
  87.                                 Boolean used[],
  88.                                 OSType appCreatorCode,
  89.                                 OSType subCreatorCode, 
  90.                                 UInt32 reserved,
  91.                                 void* reserved2);
  92.         OSStatus Stop(void);
  93.         OSStatus HandleEvent(EventRecord &theEvent, Boolean &handled);
  94.         OSStatus Show(DialogPtr theDialog, short dialogItemNumber, Rect &r);
  95.         OSStatus Hide(void);
  96.         OSStatus BeginConfiguration(UInt32 count, ISpNeed needs[]);
  97.         OSStatus EndConfiguration(Boolean accept);
  98.         OSStatus GetIcon(short &iconSuiteResourceId);
  99.         OSStatus GetState(UInt32 buflen, void *buffer, UInt32 &length);
  100.         OSStatus SetState(UInt32 length, void *buffer);
  101.         OSStatus SetActive(Boolean active);
  102.         OSStatus DialogItemHit(short itemHit);    
  103.  
  104.         OSStatus Tickle(void);
  105.         OSStatus InterruptTickle(void);
  106.         OSStatus Draw(void);
  107.         OSStatus Click(const EventRecord &event);
  108.         OSStatus ADBReInit(Boolean inPostProcess);
  109.  
  110.     public:
  111.         static OSStatus Initialize(FSSpec fileSpec);
  112.         static void Terminate(void);
  113.         
  114.         void AutoConfigure(Boolean used[]);
  115.  
  116.         Boolean    TryButtonConfig        (UInt32 inNeedIndex, Boolean inOnlyExactMatch);
  117.         Boolean    TryAxisConfig         (UInt32 inNeedIndex, Boolean inOnlyExactMatch);
  118.         Boolean    TryDPadConfig         (UInt32 inNeedIndex, Boolean inOnlyExactMatch);
  119.         Boolean    TryStickConfig         (UInt32 inNeedIndex, Boolean inOnlyExactMatch);
  120.  
  121.         Boolean    TryStickXAxisConfig (UInt32 inNeedIndex);
  122.         Boolean    TryStickYAxisConfig (UInt32 inNeedIndex);
  123.         Boolean    TryDPadXAxisConfig    (UInt32 inNeedIndex);
  124.         Boolean    TryDPadYAxisConfig    (UInt32 inNeedIndex);
  125.         Boolean    TryTrimConfig        (UInt32 inNeedIndex);
  126.         Boolean    TryThrottleConfig     (UInt32 inNeedIndex);
  127.         Boolean    TryRudderConfig     (UInt32 inNeedIndex);
  128.         Boolean    TryGasConfig         (UInt32 inNeedIndex);
  129.         Boolean    TryBrakeConfig         (UInt32 inNeedIndex);
  130.             
  131.     private:
  132.         static void UIDNotifyProc(USBDeviceNotificationParameterBlockPtr passedPB);
  133.         inline void    HandleDisappearedDevice(USBDeviceNotificationParameterBlockPtr passedPB);
  134.  
  135.         static List * gUSBDevices;
  136.         UHIDModuleDispatchTablePtr    mHIDModuleDispatchTable;
  137.         USBDeviceNotificationParameterBlock    mConnectionNotifyPB;
  138.         UHIDModuleConnectionID        mUHIDConnectionID;
  139.  
  140.         USBDeviceRef                mUSBDeviceRef;
  141.  
  142.     private:        
  143.         virtual OSStatus Patch(void);
  144.         virtual OSStatus Unpatch(void);
  145.         
  146.  
  147.     public:
  148.         void ParseData(
  149.             Ptr                        inBuffer);
  150.  
  151.     public:
  152.         
  153.         // virtual element code
  154.         void StartVirtualElements(void);
  155.         void StopVirtualElements(void);
  156.  
  157.         UInt32 * GetDialogItemToNeedMapping () { return mDialogItemToNeedMapping; };
  158.     
  159.     private:
  160.         UInt32     mDialogItemToNeedMapping[kDialogItem_NumPopups+1];    // we dont use index 0
  161.  
  162.     private:
  163.         // reverse
  164.         Boolean mReverseStick;
  165.         Boolean mReverseTrim;
  166.         Boolean mReverseThrottle;
  167.         Boolean mReverseRudder;
  168.         Boolean mReverseDPad[4];
  169.                 
  170.     private:
  171.         short mWhichDevice;
  172.  
  173.         // actual elements, nil if doesn't exist
  174.         ISpDeviceReference mDevice;
  175.         
  176.  
  177.     private:
  178.     // ui code, internal
  179.     // internal
  180.     Boolean mVisable;    
  181.     Boolean mActive;
  182.     volatile Boolean mVirtualElementsValid;
  183.     
  184.     void SetVirtualElements(void);
  185.     Boolean IsPressed (short inDialogItem);    // is this dialog item 'pressed' (ie on the device)
  186.     
  187.     Boolean mPopupPressed[kDialogItem_NumPopups];
  188.     
  189.     void UpdateSingleButton(UInt32 curButton, UInt32 index);
  190.     void UpdateAllButtons(void);
  191.  
  192. private:
  193.     void ClearVirtuals (void);
  194.         
  195.     OSStatus PushAllNonDefaultValues(void);
  196.         
  197. private:
  198.     FSSpec mFileSpec;
  199.     Rect mRect;
  200.     short mResRef;
  201.     
  202.     typedef struct
  203.     {
  204.         UInt32 version;
  205.         
  206.         UInt32 mapping[kDialogItem_NumPopups];        
  207.  
  208.         UInt32 flags;
  209.     } USBSetVersion1;
  210.  
  211.     enum
  212.     {
  213.         kSetReverseStick            =    0x00000001,
  214.         kSetReverseTrim                 =    0x00000002,
  215.         kSetReverseThrottle         =    0x00000004,
  216.         kSetReverseRudder             =    0x00000008,
  217.         kSetReverseDPad1            =    0x00000010,
  218.         kSetReverseDPad2            =    0x00000020,
  219.         kSetReverseDPad3            =    0x00000040,
  220.         kSetReverseDPad4            =    0x00000080
  221.     };
  222.  
  223.     enum 
  224.     {
  225.         kSetVersion1 = '0001'
  226.     };
  227.     
  228.     enum
  229.     {
  230.         kMappingFlag_AxisIsSymmetric =        0x00000001
  231.     };
  232.     
  233.     typedef struct
  234.     {
  235.         UInt32 usbElementKey;                    // code for what kind of thing this is
  236.         
  237.         ISpElementKind kind;                    // kind of element it is
  238.         ISpElementReference element;            // references
  239.         UInt32 lastValue;
  240.         
  241.         // (axis value - min) * multiplier = input axis value
  242.         SInt32 min;
  243.         SInt32 max;
  244.         double multiplier;
  245.         
  246.         // does this usb element map to an ISp element ?
  247.         Boolean valid;                            // false -> ignore this element
  248.         Boolean highLevelValid;                    // true -> use this for high level
  249.         UInt32 highIndex;                        // index within element kind
  250.  
  251.         UInt32 flags;                            // currently just used for axis symmetry
  252.     } USBMapping;
  253.     
  254.     UInt32 mUSBElementCount;
  255.     USBMapping * mUSBMapping;
  256.  
  257.     OSStatus BuildMapping(void);
  258.     void DisposeMapping(void);
  259.  
  260.     public:
  261.         enum { kMaxHighLevelDpads = 4, kMaxHighLevelButtons = 12 };
  262.  
  263.     private:
  264.         UInt32    mNumOrderedButtons;
  265.         UInt8    mOrderedButtonsIndecies[kMaxHighLevelButtons];
  266.  
  267.     typedef struct
  268.     {
  269.         Boolean hasXAxis;
  270.         Boolean hasYAxis;
  271.         Boolean hasTrim;
  272.         Boolean hasThrottle;
  273.         Boolean hasRudder;
  274.         Boolean hasBrake;
  275.         Boolean hasGas;
  276.  
  277.         UInt32    numDPads;
  278.         UInt32    numButtons;
  279.         
  280.         UInt32    triggerButtonIndex;
  281.         UInt32    startButtonIndex;
  282.         UInt32    selectButtonIndex;
  283.  
  284.         Boolean    is4WayDPad[kMaxHighLevelDpads];
  285.         
  286.         // these indecis are into our mJoyManagerMapping array
  287.         UInt32    xAxisIndex;
  288.         UInt32    yAxisIndex;
  289.         UInt32    trimIndex;
  290.         UInt32    throttleIndex;
  291.         UInt32    rudderIndex;
  292.         UInt32    brakeIndex;
  293.         UInt32    gasIndex;
  294.         UInt32    buttonIndex[kMaxHighLevelButtons];
  295.         UInt32    dpadIndex[kMaxHighLevelDpads];
  296.         
  297.     } HighLevelFeatures;
  298.  
  299.     HighLevelFeatures features;
  300.     void    GetFeatures (void);
  301.  
  302.     typedef struct
  303.     {
  304.         // axis
  305.         UInt32 xAxis;
  306.         UInt32 yAxis;
  307.         UInt32 trim;
  308.         UInt32 throttle;
  309.         UInt32 rudder;
  310.         UInt32 brake;
  311.         UInt32 gas;
  312.         
  313.         UInt32 xAxis_as_buttons[2];
  314.         UInt32 yAxis_as_buttons[2];
  315.         UInt32 trim_as_buttons[2];
  316.         UInt32 throttle_as_buttons[2];
  317.         UInt32 rudder_as_buttons[2];
  318.         UInt32 brake_as_button;
  319.         UInt32 gas_as_button;
  320.         
  321.         // dpads
  322.         UInt32 dpads[kMaxHighLevelDpads];
  323.         UInt32 dpads_as_buttons[kMaxHighLevelDpads][4]; // t,l,b,r
  324.         UInt32 dpads_as_axis[kMaxHighLevelDpads][2]; // x,y 
  325.         
  326.         // buttons
  327.         UInt32 buttons[kMaxHighLevelButtons];
  328.     } HighLevelData;
  329.  
  330.         void SetAsFields(HighLevelData ¤t);
  331.         void PushDataToVirtuals(HighLevelData ¤t);
  332.     
  333.     typedef struct
  334.     {
  335.         // axis
  336.         ISpElementReference xAxis;
  337.         ISpElementReference yAxis;
  338.         ISpElementReference trim;
  339.         ISpElementReference throttle;
  340.         ISpElementReference rudder;
  341.         ISpElementReference brake;
  342.         ISpElementReference gas;
  343.         
  344.         ISpElementReference stickMovement;
  345.         ISpElementReference stickDPad;
  346.         
  347.         ISpElementReference xAxis_as_buttons[2];
  348.         ISpElementReference yAxis_as_buttons[2];
  349.         ISpElementReference trim_as_buttons[2];
  350.         ISpElementReference throttle_as_buttons[2];
  351.         ISpElementReference rudder_as_buttons[2];
  352.         ISpElementReference brake_as_button;
  353.         ISpElementReference gas_as_button;
  354.         
  355.         // dpads
  356.         ISpElementReference dpads[kMaxHighLevelDpads];
  357.         ISpElementReference dpads_as_movement[kMaxHighLevelDpads];
  358.         ISpElementReference dpads_as_buttons[kMaxHighLevelDpads][4]; // t,l,b,r
  359.         ISpElementReference dpads_as_axis[kMaxHighLevelDpads][2]; // x,y 
  360.         
  361.         // buttons
  362.         ISpElementReference buttons[kMaxHighLevelButtons];
  363.     } VirtualElements;
  364.     
  365.     VirtualElements virtuals;
  366.     HighLevelData mOldHLData;
  367.     HighLevelData mCurHLData;
  368.  
  369. private:
  370. // 6/24/98 removed mOrphan, check for non-zeroness of mHIDModuleDispatchTable instead
  371. //    Boolean                        mOrphan;
  372.     Boolean                        mMustRestorePatch;
  373.     Boolean                        mPatched;
  374.  
  375. private:
  376.     void DoReverse(short dialog_item, Boolean &the_boolean);
  377.  
  378. public:
  379.     // ••• these routines _should_ be implemented by subclass
  380.     virtual OSStatus    GetSize(Point &minimum, Point &best);
  381.     virtual UInt16         GetDeviceResID(void);
  382.  
  383.     // ••• these routines must be implemented by subclass
  384. //    static Boolean        IsSubclassName(UHIDModuleDispatchTablePtr hidmoduleDispatchTable) { return false; };
  385.     virtual UInt32         GetDeviceElementCount (void);
  386.     virtual void        GetElementName (UInt32 inElementIndex, Str31 outElementName);
  387.     virtual UInt32         GetElementKey(UInt32 inElementIndex);
  388.     virtual SInt32         GetElementMinValue(UInt32 inElementIndex);
  389.     virtual SInt32         GetElementMaxValue(UInt32 inElementIndex);
  390.  
  391.     virtual SInt32         ParseElementValue(UInt32 inElementIndex, Ptr inBuffer);
  392.     
  393.     // ••• end subclass routines
  394.     
  395. };
  396.  
  397. class TMTopGun : public ISpUSBDevice
  398. {
  399.     public:
  400.         TMTopGun(const UHIDModuleDispatchTablePtr hidmoduleDispatchTable,
  401.                  const USBDeviceRef usbDeviceRef, const FSSpec &inFileSpec);
  402.         virtual ~TMTopGun();
  403.  
  404.         static Boolean    IsTMTopGun(UHIDModuleDispatchTablePtr hidmoduleDispatchTable);
  405.     
  406.         virtual OSStatus    GetSize(Point &minimum, Point &best);
  407.         virtual UInt16         GetDeviceResID(void);
  408.  
  409.         virtual UInt32        GetDeviceElementCount(void);
  410.         virtual UInt32        GetElementKey(UInt32 inElementIndex);
  411.         virtual void        GetElementName (UInt32 inElementIndex, Str31 outElementName);
  412.         virtual SInt32        GetElementMinValue(UInt32 inElementIndex);
  413.         virtual SInt32        GetElementMaxValue(UInt32 inElementIndex);
  414.  
  415.         virtual SInt32        ParseElementValue(UInt32 inElementIndex, Ptr inBuffer);
  416.         
  417.         enum
  418.         {
  419.             kTMTopGun_XAxis,            // 8 bits (-128 to 127)
  420.             kTMTopGun_YAxis,            // 8 bits (-128 to 127)
  421.  
  422.             kTMTopGun_Hat,                // 4 bits (0 is idle, 1 is up going clockwise)
  423.             kTMTopGun_Trigger,            // 1 bit
  424.             kTMTopGun_ThumbHigh,        // 1 bit
  425.             kTMTopGun_ThumbLow,            // 1 bit
  426.             kTMTopGun_Pinky,            // 1 bit
  427.             
  428.             kTMTopGun_NumElements
  429.         };
  430.  
  431.  
  432. };
  433.  
  434. class GravisPad : public ISpUSBDevice
  435. {
  436.     public:
  437.         GravisPad(const UHIDModuleDispatchTablePtr hidmoduleDispatchTable,
  438.                   const USBDeviceRef usbDeviceRef, const FSSpec &inFileSpec);
  439.         virtual ~GravisPad();
  440.  
  441.         static Boolean    IsGravisPad(UHIDModuleDispatchTablePtr hidmoduleDispatchTable);
  442.     
  443.         virtual OSStatus    GetSize(Point &minimum, Point &best);
  444.         virtual UInt16         GetDeviceResID(void);
  445.  
  446.         virtual UInt32        GetDeviceElementCount(void);
  447.         virtual UInt32        GetElementKey(UInt32 inElementIndex);
  448.         virtual void        GetElementName (UInt32 inElementIndex, Str31 outElementName);
  449.         virtual SInt32        GetElementMinValue(UInt32 inElementIndex);
  450.         virtual SInt32        GetElementMaxValue(UInt32 inElementIndex);
  451.  
  452.         virtual SInt32        ParseElementValue(UInt32 inElementIndex, Ptr inBuffer);
  453.         
  454.         enum
  455.         {
  456.             kGravisPad_DPad,            // 16 bits, 8 bit x then 8 bit y
  457.             
  458.             kGravisPad_Red,                // 1 bit
  459.             kGravisPad_Yellow,            // 1 bit
  460.             kGravisPad_Green,            // 1 bit
  461.             kGravisPad_Blue,            // 1 bit
  462.             
  463.             kGravisPad_LeftBottom,        // 1 bit
  464.             kGravisPad_RightBottom,        // 1 bit
  465.             kGravisPad_LeftTop,            // 1 bit
  466.             kGravisPad_RightTop,        // 1 bit
  467.  
  468.             kGravisPad_Select,            // 1 bit
  469.             kGravisPad_Start,            // 1 bit
  470.                         
  471.             kGravisPad_NumElements
  472.         };
  473.  
  474.  
  475. };
  476.  
  477.  
  478. #endif // __ISPSAMPLEUSB__
  479.